package com.backstage.controller.login;

import com.backstage.entity.Role;
import com.backstage.entity.UserEntity;
import com.backstage.jwt.JWTToken;
import com.backstage.service.login.LoginService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cynic on 2018/4/10.
 */
@Controller
public class LoginController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;

    @RequestMapping(value= "/login")
    @ResponseBody
    public Map login() {
        Map json = new HashMap<>();
        Subject subject = SecurityUtils.getSubject();
        JWTToken token = (JWTToken) subject.getPrincipal();
        UserEntity userEntity = token.getUserEntity();
        String userName = userEntity.getUserName();
        List<Role> roles = userEntity.getRoles();
//        Map<String, Object> json = new HashMap<>();
        json.put("userName", userName);
        json.put("roles", roles);
        json.put("token", token.getToken());
        json.put("expire_time", token.getExpireTime());
        return json;
    }
}
